/*-
 * Copyright (c) 2011 Ondrej Fischer <ondrej.fischer@gmail.com>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */
#ifndef EXPRESS_JOB_HPP
#define EXPRESS_JOB_HPP

#define synchronized
#include <string>
#include "submitservice.hpp"


using namespace std;

namespace express
{
    

    /**
     * Class Job
     * 
     * This class represents the full job. Eg. full port processing from
     * configuration to installation and cleanup
     */
    class Job {


    /* ------------------------------------------------------------------------
     * Section: Job nested classes
     */

    public:

        /**
         * Class Operation
         * 
         * Operation represents the concrete operation to be performed on a Job
         * during one task
         */
        class Operation {

            SubmitService::Target& target;

        public:
            Operation(SubmitService::Target& s): target(s) {}
            virtual void operator()(Job& job);
            inline SubmitService::Target& getTarget() {return target;}

        };

        class Noop : public Operation {
        public:
            Noop(SubmitService::Target& s): Operation(s) {}
            virtual void operator()(Job& job) {}
        };

        /**
         * Class Task
         *
         * This class represents a single task in the Job processing chain
         */
        class Task {
            
        private:

            /**
             * Job, to which the task belongs
             */
            Job& job;

            /**
             * The functional purpose of this task
             */
            Operation& operation;

            /**
             * Dependencies, that will be satisfied by submitting of this task
             * for execution
             */
            vector<Task*> submit;
            
            /**
             * Dependencies, that need this task to be finished
             */
            vector<Task*> finish;
            
            /**
             * Number of tasks, that are required to submit this task for
             * execution.
             */
            int requirements;
            

        public:
            /**
             * Task constructor must get Job and Operation as parameters. It
             * cannot exist without them.
             */
            Task(Job& j, Operation& o): job(j), operation(o), requirements(0) {}
            
            /**
             * This method adds a dependency edge between task passed as parameter
             * and this task
             */
            synchronized void require(const Task& task);
            
            /**
             * Method, that notifies the dependent task about finishing this one
             */
            void notify(const Task& task);

            virtual void process() {operation(job);}
            
            void broadcastSubmit();
            void broadcastFinish();

            void trySubmit() {}
            virtual bool quit() {return false;}
        };


        /**
         * 
         */
        class Description {
            
        public:
            virtual Job& create(string id) = 0;

        };


    /* ------------------------------------------------------------------------
     * Section: Job class members
     */
    private:

        /**
         * Unique identifier of the job
         */
        string id;


    public:
        
        /**
         * Job constructor
         */
        Job(string i): id(i) {}

    };


}

#endif // EXPRESS_JOB_HPP
